package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.AttLeave;
import com.cssl.pojo.SocInsurance;
import com.cssl.service.SocInsuranceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class InsuranceController {
    @Resource
    private SocInsuranceService service;

    @GetMapping("/select")
    private IPage<SocInsurance> select(Integer pageIndex, Integer pageSize,Integer deptid,String name){
        if (pageIndex ==null||pageIndex == 0){
            pageIndex =1;
        }
        if (pageSize == null || pageSize ==0){
            pageSize =5;
        }
        if ((name == null || name == "")&&(deptid!=null&&deptid>0)){
            IPage<SocInsurance> page=service.select(new Page<SocInsurance>(pageIndex, pageSize),
                    new QueryWrapper<SocInsurance>().eq("sd.id",deptid).eq("ss.is_deleted",0));
            return page;
        }else if((name != null && name != "")&&deptid==null){
            IPage<SocInsurance> page=service.select(new Page<SocInsurance>(pageIndex, pageSize),
                    new QueryWrapper<SocInsurance>().like("ss.name",name).eq("ss.is_deleted",0));
            return page;
        }else if ((name != null && name != "") && (deptid!=null&&deptid>0)){
            IPage<SocInsurance> page=service.select(new Page<SocInsurance>(pageIndex, pageSize),
                    new QueryWrapper<SocInsurance>().like("ss.name",name).eq("sd.id",deptid).eq("ss.is_deleted",0));
            return page;
        }
        IPage<SocInsurance> page=service.select(new Page<SocInsurance>(pageIndex, pageSize),
                new QueryWrapper<SocInsurance>().eq("ss.is_deleted",0));
        return page;
    }

    @GetMapping("/getOne")
    public SocInsurance get(Integer id){
        return service.getById(id);
    }

    @GetMapping("/update")
    public boolean update(int id,int cityid,String socialbase,String housebase,String cominjuryrate,String socialremark,String comhouserate,String perhouserate,String houseremark){
        SocInsurance insurance=new SocInsurance();
        insurance.setId(id);
        insurance.setCity_id(cityid);
        insurance.setSocial_base(socialbase);
        insurance.setHouse_base(housebase);
        insurance.setCom_injury_rate(cominjuryrate);
        insurance.setSocial_remark(socialremark);
        insurance.setCom_house_rate(comhouserate);
        insurance.setPer_house_rate(perhouserate);
        insurance.setHouse_remark(houseremark);
        System.out.println(insurance);
        if (service.updateById(insurance)){
            return true;
        }
        return false;
    }

}
